﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;
namespace TranTekBE.Upload
{
    public partial class Chart : PageRequireLogin
    {
        public string linevalue = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            TranTekBE.Maintenance.func abcd = new TranTekBE.Maintenance.func();
            DataSet liststore = FunctionsUtils.Database.ExecuteQuery("SELECT C007000 as ID, C007001 as Name FROM T0070");
            if (liststore != null)
            {
                FunctionsUtils.BindDataForControlList(idstore, liststore, "Name", "Name");
                FunctionsUtils.BindDataForControlList(storeid, liststore, "Name", "Name");
            }

            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            string now = DateTime.Now.ToString("MM/dd/yyyy");
            string endtime = DateTime.Now.AddMonths(-3).ToString("MM/dd/yyyy");
            test.Text = now;
            datebar.Text = now;
            end.Text = now;
            start.Text = endtime;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<Data> getdata(string date, string store)
        {
            List<Data> dataList = new List<Data>();
            string sql = "SELECT TOP 1 * FROM tbl_Upload WHERE NgayUpload='" + date + "' AND CuaHang = '" + store + "' ORDER BY ID DESC";
            DataSet chartmm = FunctionsUtils.Database2.ExecuteQuery(sql);
            if (chartmm != null)
            {
                foreach (DataRow row in chartmm.Tables[0].Rows)
                {
                    dataList.Add(new Data("Bán nhanh", int.Parse(row[1].ToString())));
                    dataList.Add(new Data("Bán vừa", int.Parse(row[2].ToString())));
                    dataList.Add(new Data("Bán chậm", int.Parse(row[3].ToString())));
                    dataList.Add(new Data("Bán nhanh", int.Parse(row[4].ToString())));
                    dataList.Add(new Data("Bán vừa", int.Parse(row[5].ToString())));
                    dataList.Add(new Data("Bán chậm", int.Parse(row[6].ToString())));
                }
            }
            return dataList;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<Data> getnow()
        {
            List<Data> dataList = new List<Data>();
            string now = DateTime.Now.ToString("MM/dd/yyyy");
            string sql = "SELECT TOP 1 * FROM tbl_Upload WHERE NgayUpload='" + now + "' AND CuaHang = 'SGP' ORDER BY ID DESC";
            DataSet chartmm = FunctionsUtils.Database2.ExecuteQuery(sql);
            if (chartmm != null)
            {
                foreach (DataRow row in chartmm.Tables[0].Rows)
                {
                    dataList.Add(new Data("Bán nhanh", int.Parse(row[1].ToString())));
                    dataList.Add(new Data("Bán vừa", int.Parse(row[2].ToString())));
                    dataList.Add(new Data("Bán chậm", int.Parse(row[3].ToString())));
                    dataList.Add(new Data("Bán nhanh", int.Parse(row[4].ToString())));
                    dataList.Add(new Data("Bán vừa", int.Parse(row[5].ToString())));
                    dataList.Add(new Data("Bán chậm", int.Parse(row[6].ToString())));
                }
            }
            return dataList;
        }

        //biểu cột
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<Data2> getbar(string date)
        {
            List<Data2> dataList = new List<Data2>();
            string sql = "SELECT * FROM tbl_Upload WHERE NgayUpload = '" + date + "' ORDER BY ID DESC";
            DataSet chartmm = FunctionsUtils.Database2.ExecuteQuery(sql);
            if (chartmm != null)
            {
                foreach (DataRow row in chartmm.Tables[0].Rows)
                {
                    int tong1 = int.Parse(row[4].ToString());
                    int tong2 = int.Parse(row[5].ToString());
                    int tong3 = int.Parse(row[6].ToString());
                    double avg = Math.Round((double)((double)tong1 * 100) / ((double)tong1 + (double)tong3),2);//?????
                    dataList.Add(new Data2(row[9].ToString(), tong1, tong2, tong3, avg, Math.Round((100 - avg), 2), string.Format("{0:0,0 đ}", tong1), string.Format("{0:0,0 đ}", tong2), string.Format("{0:0,0 đ}", tong3)));
                }
            }
            return dataList;
        }
        //biểu cột mặc định
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<Data2> getbarnow()
        {
            List<Data2> dataList = new List<Data2>();
            string now = DateTime.Now.ToString("MM/dd/yyyy");
            string sql = "SELECT * FROM tbl_Upload WHERE NgayUpload = '" + now + "' ORDER BY ID DESC";
            DataSet chartmm = FunctionsUtils.Database2.ExecuteQuery(sql);
            if (chartmm != null)
            {
                foreach (DataRow row in chartmm.Tables[0].Rows)
                {
                    int tong1 = int.Parse(row[4].ToString());
                    int tong2 = int.Parse(row[5].ToString());
                    int tong3 = int.Parse(row[6].ToString());
                    int tong = tong1 + tong3;
                    double avg = Math.Round((double)((double)tong1 * 100) / ((double)tong1 + (double)tong3), 2);//?????
                    dataList.Add(new Data2(row[9].ToString(), tong1, tong2, tong3, avg,  Math.Round((100 - avg),2), string.Format("{0:0,0 đ}", tong1), string.Format("{0:0,0 đ}", tong2), string.Format("{0:0,0 đ}", tong3)));
                }
            }
            return dataList;
        }
        //biểu cột mặc định
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<Data3> getline(string store, string start, string end)
        {
            List<Data3> dataList = new List<Data3>();
            string sql = "SELECT TOP 12 * FROM tbl_Upload WHERE CuaHang = '" + store + "' AND NgayUpload >= '" + start + "' AND NgayUpload <= '" + end + "' ORDER BY NgayUpload ASC";
            DataSet chartmm = FunctionsUtils.Database2.ExecuteQuery(sql);
            if (chartmm != null)
            {
                foreach (DataRow row in chartmm.Tables[0].Rows)
                {
                    dataList.Add(new Data3(row[8].ToString().Split(' ')[0], int.Parse(row[4].ToString().Trim()), string.Format("{0:0,0 đ}", int.Parse(row[4].ToString().Trim())), int.Parse(row[5].ToString().Trim()), string.Format("{0:0,0 đ}", int.Parse(row[5].ToString().Trim())), int.Parse(row[6].ToString().Trim()), string.Format("{0:0,0 đ}", int.Parse(row[6].ToString().Trim()))));
                }
            }
            return dataList;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<Data3> getnowline()
        {
            List<Data3> dataList = new List<Data3>();
            string sql = "SELECT TOP 12 * FROM tbl_Upload WHERE CuaHang = 'SGP' ORDER BY NgayUpload ASC";
            DataSet chartmm = FunctionsUtils.Database2.ExecuteQuery(sql);
            if (chartmm != null)
            {
                foreach (DataRow row in chartmm.Tables[0].Rows)
                {
                    dataList.Add(new Data3(row[8].ToString().Split(' ')[0], int.Parse(row[4].ToString().Trim()), string.Format("{0:0,0 đ}", int.Parse(row[4].ToString().Trim())), int.Parse(row[5].ToString().Trim()), string.Format("{0:0,0 đ}", int.Parse(row[5].ToString().Trim())), int.Parse(row[6].ToString().Trim()), string.Format("{0:0,0 đ}", int.Parse(row[6].ToString().Trim()))));
                }
            }
            return dataList;
        }
    }
    public class Data
    {
        public string ColumnName = "";
        public int Value = 0;

        public Data(string columnName, int value)
        {
            ColumnName = columnName;
            Value = value;
        }
    }
    public class Data2
    {
        public string ColumnName = "";
        public int Value1 = 0;
        public int Value2 = 0;
        public int Value3 = 0;
        public double Value4 = 0;
        public double Value5 = 0;
        public string ColumnName1 = "";
        public string ColumnName2 = "";
        public string ColumnName3 = "";
        public Data2(string columnName, int value1, int value2, int value3, double value4, double value5, string columnName1, string columnName2, string columnName3)
        {
            ColumnName = columnName;
            ColumnName1 = columnName1;
            ColumnName2 = columnName2;
            ColumnName3 = columnName3;
            Value1 = value1;
            Value2 = value2;
            Value3 = value3;
            Value4 = value4;
            Value5 = value5;
        }
    }
    public class Data3
    {
        public string ColumnName1 = "";
        public string ColumnName2 = "";
        public string ColumnName3 = "";
        public string ColumnName4 = "";
        public int Value1 = 0;
        public int Value2 = 0;
        public int Value3 = 0;

        public Data3(string columnName1, int value1, string columnName2, int value2, string columnName3, int value3, string columnName4)
        {
            ColumnName1 = columnName1;
            ColumnName2 = columnName2;
            ColumnName3 = columnName3;
            ColumnName4 = columnName4;
            Value1 = value1;
            Value2 = value2;
            Value3 = value3;
        }
    }
}